package common.com.androidbase.datarequest;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.sohu.sdk.common.toolbox.DeviceUtils;
import com.android.sohu.sdk.common.toolbox.ListUtils;
import com.android.sohu.sdk.common.toolbox.LogUtils;
import com.common.sdk.net.connect.http.OkhttpManager;
import com.common.sdk.net.connect.http.SohuNetConfig;
import com.common.sdk.net.connect.http.center.ErrorType;
import com.common.sdk.net.connect.http.model.HttpError;
import com.common.sdk.net.connect.http.model.OkHttpSession;
import com.common.sdk.net.connect.http.util.OkhttpCacheUtil;
import com.common.sdk.net.connect.interfaces.IResponseListener;
import com.common.sdk.net.connect.interfaces.impl.DefaultResponseListener;
import com.common.sdk.net.connect.interfaces.impl.NoThingToDoParser;
import com.sohu.trafficstatistics.util.TrafficStatisticUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import common.com.androidbase.R;
import common.com.androidbase.datarequest.view.DataResponseDetail;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;

public class DataRequestActivity extends Activity {

    private static final String TAG = "DataRequestActivity";

    OkhttpManager okhttpManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_delete_cache);

        List<Interceptor> interceptors = new ArrayList<>();
        interceptors.add(new LoggingInterceptor());
        OkhttpManager.init(this, new SohuNetConfig.Builder(this).setInterceptors(interceptors).build());
        OkhttpManager.setBackupDomain("bks1.api.tv.itc.cn");
        OkhttpManager.addReplacableDomain("s1.api.tv.itc.cn");

        Map<String, Object> baseParams = new HashMap<String, Object>();

        baseParams.put("gid", "02ffff10741000e766663554ca0c1da4e0d103651e9386");
        baseParams.put("api_key", "9854b2afa779e1a6bff1962447a09dbd");
        baseParams.put("appid", "107402");
        baseParams.put("ua", "Mozilla/5.0%20(Linux;%20Android%205.0.1;%20MX4%20Pro%20Build/LRX22C)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Version/4.0%20Chrome/40.0.2214.114%20Mobile%20Safari/537.36%20SohuVideoMobile/6.9.8%20(Platform/AndroidPhone;%20Android/5.0.1)");
        baseParams.put("plat", "6");
        baseParams.put("poid", "1");
        baseParams.put("partner", "93");
        baseParams.put("sver", "6.9.8");
        baseParams.put("sysver", "5.0.1");
        // 设置基础参数，后面可使用SohuRequestBuilder构建request时可以决定是否需要传递基础参数
        OkhttpManager.setBaseParams(baseParams);

        okhttpManager = new OkhttpManager();

        Button button = (Button)findViewById(R.id.btn_test);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//            ThreadTools.startNormalThread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        for (int i = 0; i < 100; i++) {
//                            startTestRequest();
//                            Thread.sleep(20);
//                        }
//                    } catch (Exception e) {
//                        LogUtils.e(TAG, e);
//                    }
//                }
//            });

//            startTestRequest();

                sendHttpRequest();

            }
        });

        Button deleteButton = (Button) findViewById(R.id.btn_test_delete);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    WifiManager wifi = (WifiManager) DataRequestActivity.this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                    WifiInfo info = null;
                    if (wifi != null) {
                        info = wifi.getConnectionInfo();
                    }
                    if (info != null) {
                        LogUtils.d(TAG, "" + info.getRssi());
                    }
                } catch (Exception e) {
                    LogUtils.e(TAG, e);
                }
//                DaylilyRequest request = DataRequestUtil.getVideoDetail(9016000644l, 2750002478l, 1);
//                new RequestManagerEx().deleteDataCache(request);
            }
        });
    }

    private void startTestJsonRequest() {
        String json = "{\n" +
                "\"mfov\":\"HUAWEI MT7-TL10\",\n" +
                "\"plat\":\"6\",\n" +
                "\"sysver\":\"4.4.2\",\n" +
                "\"sys\":\"android\",\n" +
                "\"mfo\":\"HUAWEI\",\n" +
                "\"poid\":\"1\",\n" +
                "\"sver\":\"1.0\",\n" +
                "\"partner\":\"93\",\n" +
                "\"deviceId\":\"867600028128337\",\n" +
                "\"deviceName\":\"test\",\n" +
                "\"requestType\":\"homeGet\",\n" +
                "\"passportId\":\"123\",\n" +
                "\"token\":\"123\"\n" +
                "}";
    }

    private void startTestRequest() {

        final Request request = DataRequestUtil.getVideoDetailRequest(9016000644000l, 2750002478000l, 1);

        okhttpManager.enqueue(request, new IResponseListener() {
            @Override
            public void onSuccess(Object notNullData, OkHttpSession session) {
                LogUtils.d(TAG, "request success , use cache : " + false);
            }

            @Override
            public void onFailure(HttpError errorCode, OkHttpSession session) {
                LogUtils.d(TAG, "request fail : " + errorCode);
            }

            @Override
            public void onCancelled(OkHttpSession session) {

            }
        }, new NoThingToDoParser());


    }

    @Override
    protected void onDestroy() {
        if (okhttpManager != null) {
            okhttpManager.cancel();
        }
        LogUtils.d(TAG, "datarequestactivity's ondestory invoked");
        super.onDestroy();
    }

    public void sendHttpRequest() {
        Request request = DataRequestUtil.getChannelV4PgcSubsData("0");
//        ColumnPgcSubsDataResultParser parser = new ColumnPgcSubsDataResultParser(is56Hot);
        CacheControl cache = OkhttpCacheUtil.buildPull2RefreshCache();
        okhttpManager.enqueue(request, new DefaultResponseListener() {

            @Override
            public void onFailure(HttpError arg0, OkHttpSession session) {
                LogUtils.d(TAG, "onFail");
            }

            @Override
            public void onSuccess(Object arg0, OkHttpSession session) {
                LogUtils.d(TAG, "onSuccess");
            }
        }, new NoThingToDoParser(), cache);
    }
}
